﻿using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Windvoice
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterManager.RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterManager.RegisterRoutes(RouteTable.Routes);
            RegisterManager.RegisterWebApi(GlobalConfiguration.Configuration);
            RegisterManager.RegisterBundles(BundleTable.Bundles);

            //initialize active record config and assemblies
            XmlConfigurationSource source = new XmlConfigurationSource(Server.MapPath("~/Config/activerecord.xml"));
            var Assemblies = new Assembly[] { System.Reflection.Assembly.GetAssembly(typeof(Windvoice.Blog.Category)), System.Reflection.Assembly.GetAssembly(typeof(Windvoice.Albums.Album)), System.Reflection.Assembly.GetAssembly(typeof(Windvoice.Authorize.User)) };
            ActiveRecordStarter.Initialize(Assemblies, source);

            //set log4net config
            log4net.Config.XmlConfigurator.Configure();

            //enable mono wcf
            //if (Environment.GetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT") != "yes")
            //{
            //    Environment.SetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT", "yes");
            //}
        }
    }



    public class RegisterManager
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            string[] nameSpace = new string[] { "Windvoice.Controllers.Page" };

            //Blog Route
            routes.MapRoute(
                "Blog", // Route name
                "Blogs", // URL with parameters
                new { controller = "Blog", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                nameSpace
            );

            routes.MapRoute(
                "BlogList", // Route name
                "Blogs/{key}", // URL with parameters
                new { controller = "Blog", action = "Index", key = UrlParameter.Optional }, // Parameter defaults
                nameSpace
            );

            routes.MapRoute(
                "BlogDetail", // Route name
                "Blog/{key}", // URL with parameters
                new { controller = "Blog", action = "Detail", key = UrlParameter.Optional }, // Parameter defaults
                nameSpace
            );

            //Album Route
            routes.MapRoute(
                "Album", // Route name
                "Album", // URL with parameters
                new { controller = "Album", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                nameSpace
            );

            routes.MapRoute(
                "AlbumDetail", // Route name
                "Album/{id}", // URL with parameters
                new { controller = "Album", action = "Detail", id = UrlParameter.Optional }, // Parameter defaults
                nameSpace
            );

            routes.MapRoute(
                "PhotoDetail", // Route name
                "Album/Photo/{id}", // URL with parameters
                new { controller = "Album", action = "Photo", id = UrlParameter.Optional }, // Parameter defaults
                nameSpace
            );

            //Resume
            //routes.MapRoute(
            //    "Resume", // Route name
            //    "Experiences", // URL with parameters
            //    new { controller = "Home", action = "Resume" }, // Parameter defaults
            //    new string[] { "Windvoice.Controllers" }
            //);
            routes.MapRoute(
                "Links", // Route name
                "Links", // URL with parameters
                new { controller = "Links", action = "Index" }, // Parameter defaults
                nameSpace
            );
            routes.MapRoute(
                "About", // Route name
                "About", // URL with parameters
                new { controller = "About", action = "Index" }, // Parameter defaults
                nameSpace
            );

            routes.MapRoute(
                "404", // Route name
                "404", // URL with parameters
                new { controller = "Home", action = "_404" }, // Parameter defaults
                nameSpace
            );
            routes.MapRoute(
                "500", // Route name
                "500", // URL with parameters
                new { controller = "Home", action = "_500" }, // Parameter defaults
                nameSpace
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                nameSpace
            );
        }

        public static void RegisterBundles(BundleCollection bundles)
        {
            Func<string[], string[]> GetScriptPath = (names) =>
            {
                const string ScriptRoot = "~/Content/js/{0}.js";
                for (int i = 0; i < names.Length; i++)
                    names[i] = String.Format(ScriptRoot, names[i]);
                return names;
            };


            bundles.Add(new ScriptBundle("~/js/jquery").Include(GetScriptPath(new string[] { "jquery", "jquery.cookie", "jquery.lightbox", "jquery.mousewheel", "jquery.qtip", "jquery.uploadify", "jquery.validate", "jquery.validate.unobtrusive" })));
            bundles.Add(new ScriptBundle("~/js/media").Include(GetScriptPath(new string[] { "silverlight", "swfobject" })));
            bundles.Add(new ScriptBundle("~/js/common").Include(GetScriptPath(new string[] { "common" })));

            bundles.Add(new StyleBundle("~/css/site").Include("~/Content/css/site.css"));
            bundles.Add(new StyleBundle("~/css/manage").Include("~/Content/css/manage.css"));

            bundles.Add(new StyleBundle("~/css/themes").Include("~/Content/css/jquery.lightbox.css", "~/Content/css/jquery.qtip.css", "~/Content/css/uploadify.css"));
        }

        public static void RegisterWebApi(HttpConfiguration config)
        {
            string[] nameSpace = new string[] { "Windvoice.Controllers.Api" };

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{key}",
                defaults: new { key = RouteParameter.Optional }
            );
        }
    }
}